package com.example.myproject.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.myproject.model.CommentLike;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface CommentLikeMapper extends BaseMapper<CommentLike> {
    
    @Insert("INSERT INTO comment_like (comment_id, user_id, create_time) VALUES " +
            "(#{commentId}, #{userId}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "likeId")
    int insert(CommentLike like);
    
    @Delete("DELETE FROM comment_like WHERE comment_id = #{commentId} AND user_id = #{userId}")
    int deleteByCommentIdAndUserId(@Param("commentId") Long commentId, @Param("userId") Long userId);
    
    @Select("SELECT COUNT(*) FROM comment_like WHERE comment_id = #{commentId} AND user_id = #{userId}")
    int existsByCommentIdAndUserId(@Param("commentId") Long commentId, @Param("userId") Long userId);
    
    @Select("SELECT COUNT(*) FROM comment_like WHERE comment_id = #{commentId}")
    int countByCommentId(@Param("commentId") Long commentId);
    
    @Update("UPDATE comment SET likes_count = (SELECT COUNT(*) FROM comment_like WHERE comment_id = #{commentId}) " +
            "WHERE comment_id = #{commentId}")
    int updateCommentLikesCount(@Param("commentId") Long commentId);
}
